package com.example.javasample.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.example.javasample.domain.UserDO;
import com.example.javasample.service.IUserService;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import com.github.xiaoymin.knife4j.annotations.ApiSupport;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springdoc.core.annotations.ParameterObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * UserController
 *
 * @author Kylin
 */
@ApiSupport(order = 3)
@Tag(name = "用户")
@RestController
@RequestMapping("/user")
public class UserController {

    @Autowired
    private IUserService userService;

    @ApiOperationSupport(order = 1)
    @Operation(summary = "列表", description = "")
    @GetMapping(value = "/list")
    public List<UserDO> list(@ParameterObject UserDO user) {
        LambdaQueryWrapper<UserDO> queryWrapper = new LambdaQueryWrapper<>();
        if (user != null) {
            if (user.getName() != null) {
                queryWrapper.like(UserDO::getName, user.getName());
            }
        }
        return userService.list(queryWrapper);
    }

    @ApiOperationSupport(order = 2)
    @Operation(summary = "新增", description = "")
    @PostMapping("/add")
    public boolean add(@ParameterObject UserDO user) {
        return userService.save(user);
    }

    @ApiOperationSupport(order = 3)
    @Operation(summary = "编辑", description = "")
    @PutMapping("/edit")
    public boolean edit(@ParameterObject UserDO user) {
        return userService.updateById(user);
    }

    @ApiOperationSupport(order = 4)
    @Operation(summary = "删除", description = "")
    @DeleteMapping("/delete/{userId}")
    public boolean delete(@PathVariable Long userId) {
        return userService.removeById(userId);
    }

    @ApiOperationSupport(order = 5)
    @Operation(summary = "批量新增", description = "")
    @PostMapping("/batchAdd")
    public boolean batchAdd(@RequestBody List<UserDO> userList) {
        return userService.saveBatch(userList);
    }

    @ApiOperationSupport(order = 6)
    @Operation(summary = "详情", description = "")
    @GetMapping("/detail/{userId}")
    public UserDO detail(@PathVariable("userId") Long userId) {
        return userService.detail(userId);
    }

}
